<?php
/**
 * 图片和二维码合并
 * @author guoxiaoyong
 */
namespace bc\util;

class ImageMergeQrcode
{

	protected $backgroundImage;
	
	protected $qrcodeImage;
	
	protected $destImage;
	
	
	/**
	 * 二维码对应的x坐标位
	 * @var int
	 */
	protected $x = 0;
	
	
	/**
	 * 二维码对应的y坐标位
	 * @var int
	 */
	protected $y = 0;
	

	/**
	 * @return int $x
	 */
	public function getX()
	{
		return $this->x;
	}

	/**
	 * @return int $y
	 */
	public function getY()
	{
		return $this->y;
	}

	/**
	 * @param int $x
	 */
	public function setX($x)
	{
		$this->x = (int)$x;
	}

	/**
	 * @param int $y
	 */
	public function setY($y)
	{
		$this->y = (int)$y;
	}

	/**
	 * @return the $destImage
	 */
	public function getDestImage()
	{
		return $this->destImage;
	}

	/**
	 * @param field_type $destImage
	 */
	public function setDestImage($destImage)
	{
		$this->destImage = $destImage;
	}

	/**
	 * 
	 * @param string $backgroupImage
	 * @param string $qrcodeImage
	 */
	public function __construct($backgroundImage, $qrcodeImage)
	{
		$this->backgroundImage = $backgroundImage;
		$this->qrcodeImage = $qrcodeImage;
	}
	
	/**
	 * 获取图片扩展名
	 * @param string $file
	 */
	protected function getExtension($file)
	{
		return pathinfo ( $file, PATHINFO_EXTENSION );
	}
	
	
	/**
	 * @return resource
	 */
	protected function createBackgroundImage()
	{
		$suffix = $this->getExtension($this->backgroundImage);
		
		switch ($suffix)
		{
			case 'png':
				$imagecreate = imagecreatefrompng($this->backgroundImage);
				break;
			case 'jpeg':
				$imagecreate = imagecreatefromjpeg($this->backgroundImage);
				break;
			case 'jpg':
				$imagecreate = imagecreatefromjpeg($this->backgroundImage);
				break;
			case 'gif':
				$imagecreate = imagecreatefromgif($this->backgroundImage);
				break;
		}
		
		return $imagecreate;
	}
	
	protected function createQrcodeImage()
	{
		$suffix = $this->getExtension($this->qrcodeImage);
		
		switch ($suffix)
		{
			case 'png':
				$imagecreate = imagecreatefrompng($this->qrcodeImage);
				break;
			case 'jpeg':
				$imagecreate = imagecreatefromjpeg($this->qrcodeImage);
				break;
			case 'jpg':
				$imagecreate = imagecreatefromjpeg($this->qrcodeImage);
				break;
			case 'gif':
				$imagecreate = imagecreatefromgif($this->qrcodeImage);
				break;
		}
		
		return $imagecreate;
	}
	
	
	
	/**
	 * @return void
	 * 生成合并图
	 */
	public function create($newImage)
	{
		
		$backgroundImageResource = $this->createBackgroundImage();

		$qrcodeImageResource = $this->createQrcodeImage();
		
		$newImageResource = imageCreatetruecolor ( imagesx ( $backgroundImageResource ), imagesy ( $backgroundImageResource ) );
		
		$color = imagecolorallocate ( $newImageResource, 255, 255, 255 );
		imagefill ( $newImageResource, 0, 0, $color );
		imageColorTransparent ( $newImageResource, $color );
		
		imagecopyresampled ( $newImageResource, $backgroundImageResource, 0, 0, 0, 0, imagesx ( $backgroundImageResource ), imagesy ( $backgroundImageResource ), imagesx ( $backgroundImageResource ), imagesy ( $backgroundImageResource ) );
		// 再将装备图片copy到已经具有人物图像的真彩色画布中，同样也不会失真
		imagecopymerge ( $newImageResource, $qrcodeImageResource, $this->getX(), $this->getY(), 0, 0, imagesx ( $qrcodeImageResource ), imagesy ( $qrcodeImageResource ), 100 );
		
		switch ($this->getExtension($newImage))
		{
			case 'png':
				$imagecreate = imagepng($newImageResource, $newImage);
				break;
			case 'jpeg':
				$imagecreate = imagejpeg($newImageResource, $newImage);
				break;
			case 'jpg':
				$imagecreate = imagejpeg($newImageResource, $newImage);
				break;
			case 'gif':
				$imagecreate = imagegif($newImageResource, $newImage);
				break;
		}
		
		imagedestroy ( $newImageResource );
	}
	
}


